// 获取附近站点数据
function getSiteListGis(start_longitude, end_longitude, start_latitude, end_Latitude) {
	var locationData = JSON.parse(localStorage.getItem('projectData'));
	var logdata = JSON.parse(localStorage.getItem('LoginData'));

	var page = "0";
	var userName = logdata.userLogin;
	var netId = locationData.netId;
	var projectName = locationData.projectName;

	var start_longitude = start_longitude;
	var end_longitude = end_longitude;
	var start_latitude = start_latitude;
	var end_latitude = end_Latitude;

	var size = "100";
	var flag = "1";

	$.ajax({
		type: "POST",
		url: url_prefix + "getSiteListGis.action?",
		dataType: 'JSON',
		data: {
			//				"page": "0",
			//	"searchKey": "家",
			//	"userName": "admin",
			//	"netId": "1",
			//	"projectName": "上海联通",
			//	"size": "10"
			'request': "{\"page\":\"" + page + "\",\"userName\":\"" + userName + "\",\"netId\":\"" + netId + "\",\"projectName\":\"" + projectName + "\",\"size\":\"" + size + "\",\"flag\":\"" + flag + "\",\"end_latitude\":\"" + end_Latitude + "\",\"end_longitude\":\"" + end_longitude + "\",\"start_latitude\":\"" + start_latitude + "\",\"start_longitude\":\"" + start_longitude + "\"}"

		},

		success: function(data) {
			var str1 = data["result"]; //解密
			//			var params = str1.replace(/\+/g, " "); //去掉字符串里的+号
			//			var str2 = UTFTranslate.ReChange(chineseFromUtf8Url(params)); //转utf8
			//			var datas = JSON.stringify(str2); //转JSON字符串
			//			var aa = datas.replace(/\\n/g, " "); //去掉换行
			//			var objs = eval('(' + aa + ')');
			//			var bb = eval('(' + objs + ')');

			if(str1.head.status == 1) {
				//				selectSite(str1.data);
				console.log(str1);
				localStorage.setItem('getSiteListGis', JSON.stringify(str1.data));
				showAnimotion(BMAP_HYBRID_MAP);
				//              showAnimotion(BMAP_HYBRID_MAP);

			} else {
				mui.alert(str1.head.info);
			}
		}

	});
}

function seachOrientation() {
	if(null == document.getElementById("seachText").value || '' == document.getElementById("seachText").value) {
		return;
	}
	var locationData = JSON.parse(localStorage.getItem('projectData'));
	var logdata = JSON.parse(localStorage.getItem('LoginData'));
	var inputTest = $("#seachText").val();
	var page = "0";
	var userName = logdata.userLogin;
	var netId = locationData.netId;
	var projectName = locationData.projectName;
	var size = "100";
	var flag = "1";
	$.ajax({
		type: "POST",
		url: url_prefix + "searchSiteListGis.action?",
		dataType: 'JSON',
		data: {
			//				"page": "0",
			//	"searchKey": "家",
			//	"userName": "admin",
			//	"netId": "1",
			//	"projectName": "上海联通",
			//	"size": "10"
			'request': "{\"searchKey\":\"" + inputTest + "\",\"page\":\"" + page + "\",\"userName\":\"" + userName + "\",\"netId\":\"" + netId + "\",\"projectName\":\"" + projectName + "\",\"size\":\"" + size + "\",\"flag\":\"" + flag + "\"}"

		},
		success: function(data) {
			var str1 = data["result"]; //解密
			if(str1.head.status == 1) {
				console.log(str1);
				insertDataforv(str1.data);
			} else {
				alert(str1.head.info);
				if(str1.head.info == "登录已失效!") {
					window.location.href = 'login.html';
				}
			}

		}
	});
}

function insertDataforv(str1) {
	localStorage.setItem('siteData', JSON.stringify(str1));
	var noApplicationRecord = document.getElementById('noApplicationRecord');
	if(str1.length * 35 > 450) {
		noApplicationRecord.style = "overflow-x:hidden;overflow-y:auto;height: 450px;"
	}

	function setDiv(item, i) {
		var div = '<div id="' + i + '" onclick="selectAction(' + i + ')" class="mohucx"><p>' +
			item.cellName +
			'</p></div>'
		return div;
	}

	function getnoApplicationData() {
		var html = ''
		for(var i = 0; i < str1.length; i++) {
			html += setDiv(str1[i], i)
		}
		noApplicationRecord.innerHTML = html
	}
	window.onload = getnoApplicationData();

}

var isseach;

function selectAction(i) {
	var seachData = JSON.parse(localStorage.getItem('siteData'));
	var sss = seachData[i];
	localStorage.setItem('cellinfo', JSON.stringify(sss));
	hideDiv();
	isseach = 1;
}

var wid = null;
var iscan;

function hideDiv() {
	var div1 = document.getElementById("guanbi");
	div1.style.display = 'none';

	iscan = 1;
	var btnArray = ['确定'];
	mui.confirm('请您先拿手机绕8字进行校正指南针再正式测量!', '', btnArray, function(e) {
		canTestdirection();
		//                  添加点击屏幕事件

		document.addEventListener('touchmove', function(event) {
			event.preventDefault(); //阻止触摸事件的默认行为，即阻止滚屏
			// 如果这个元素的位置内只有一个手指的话

			iscan = !iscan;
			if(iscan == 0) {
				plus.orientation.clearWatch(wid);
				wid = null;
				document.getElementById('dianjitishi').innerHTML = '滑动屏幕任意位置开始测量';

			} else {
				canTestdirection();
				document.getElementById('dianjitishi').innerHTML = '任意滑动屏幕锁定测量值';
			}

		}, false);

	})

}

function hideseach() {
	var div1 = document.getElementById("guanbi");
	div1.style.display = 'none';
}

// 测试方位角
function canTestdirection() {
	document.oncontextmenu = function() {
		return YES;
	};
	t = document.getElementById("compass");
	x = document.getElementById("infsssss");

	if(plus.os.name == "iOS") {
		t.style.webkitTransition = "all 0.1s linear";
		bani = true;
		if(plus.device.model.indexOf("iPhone") < 0) {
			bsp = false;
			document.getElementById("tip").innerText = "此设备方向数据不准确";

		}
	}
	wid = plus.orientation.watchOrientation(function(r) {
		update(bsp ? r.trueHeading : r.alpha); //update(r.alpha);

	}, function(e) {
		// error
	}, {
		frequency: 100
	});

	document.onselectstart = function() {
		return false;
	}

}

// 上传测试的方位角数据
function sureTestData() {

	//	if(iscan==1){
	//		mui.alert('请先锁定测量值','提示','确定');
	//		return;
	//	}
	var cellinfo = JSON.parse(localStorage.getItem('cellinfo'));
	shangchuanData(cellinfo, aimuthint);
	var btnArray;
	if(isseach == 1) {
		btnArray = ['重新测试', '上传数据'];
		var azimuth = $("#infsssss").text();
		var aimuthint = parseInt(azimuth);
		console.log(azimuth);
		mui.confirm('初始数据：' + cellinfo.azimuth + ' \n  测试数据: ' + aimuthint + '', '测试结果', btnArray, function(e) {
			if(e.index == 1) {
				shangchuanData(cellinfo, aimuthint);
			} else {

			}
		})
	} else {
		btnArray = ['重新测试'];
		var azimuth = $("#infsssss").text();
		var aimuthint = parseInt(azimuth);
		mui.alert('测试数据: ' + aimuthint + '', '测试结果', '确定');

	}
}

// 上传数据
function shangchuanData(data, azimuth) {
	var locationData = JSON.parse(localStorage.getItem('projectData'));
	var logdata = JSON.parse(localStorage.getItem('LoginData'));
	var token = logdata.token;
	var projectId = locationData.projectId;
	var ci = data.ci;
	var projectName = locationData.projectName;
	var netId = data.netId;
	var azimuth = azimuth;

	$.ajax({
		type: "POST",
		url: url_prefix + 'updateAntennaInfo.action?',
		dataType: 'JSON',
		data: {

			'request': "{\"ci\":\"" + ci + "\",\"token\":\"" + token + "\",\"projectId\":\"" + projectId + "\",\"projectName\":\"" + projectName + "\",\"azimuth\":\"" + azimuth + "\",\"netId\":\"" + netId + "\"}"

		},
		success: function(data) {
			var str1 = data["result"]; //解密
			console.log(str1);
			if(str1.head.status == 1) {
				console.log(str1);
				mui.alert('数据上传成功', '提示');

			} else {
				mui.alert(str1.head.info);
				if(str1.head.info == "登录已失效!") {
					window.location.href = 'login.html';
				}
			}
		}
	});
}

// PCI优化搜索
function pciseach() {
	if(null == document.getElementById("pciseachText").value || '' == document.getElementById("pciseachText").value) {
		return;
	}
	var locationData = JSON.parse(localStorage.getItem('projectData'));
	var logdata = JSON.parse(localStorage.getItem('LoginData'));
	var inputTest = $("#pciseachText").val();
	var page = "0";
	var userName = logdata.userLogin;
	var netId = locationData.netId;
	var projectName = locationData.projectName;
	var size = "100";
	var flag = "1";

	$.ajax({
		type: "POST",
		url: url_prefix + "searchSiteListGis.action?",
		dataType: 'JSON',
		data: {
			'request': "{\"searchKey\":\"" + inputTest + "\",\"page\":\"" + page + "\",\"userName\":\"" + userName + "\",\"netId\":\"" + netId + "\",\"projectName\":\"" + projectName + "\",\"size\":\"" + size + "\",\"flag\":\"" + flag + "\"}"

		},
		success: function(data) {
			var str1 = data["result"]; //解密

			if(str1.head.status == 1) {
				console.log(str1);
				pciselectSite(str1.data);

			} else {
				alert(str1.head.info);
				if(str1.head.info == "登录已失效!") {
					window.location.href = 'login.html';
				}
			}

		}
	});
}

function pciselectSite(str1) {
	var tongpindatadiv = document.getElementById('tongpindatadiv');
	tongpindatadiv.style.display = 'none';

	localStorage.setItem('pcisiteData', JSON.stringify(str1));
	var PCIseachdiv = document.getElementById('PCIseach');
	PCIseachdiv.style.display = 'block';
	if(str1.length * 35 > 450) {
		PCIseachdiv.style = "overflow-x:hidden;overflow-y:auto;height: 450px;"
	}

	function setDiv(item, i) {
		var div = '<div id="' + i + '" onclick="pciselectAction(' + i + ')" class="mohucx"><p>' +
			item.cellName +
			'</p></div>'
		return div;
	}

	function getnoApplicationData() {
		var html = ''
		for(var i = 0; i < str1.length; i++) {
			html += setDiv(str1[i], i)
		}
		PCIseachdiv.innerHTML = html
	}
	window.onload = getnoApplicationData();

}

function pciselectAction(i) {
	var PCIseachdiv = document.getElementById('PCIseach');
	PCIseachdiv.style.display = 'none';
	var pcidata = JSON.parse(localStorage.getItem('pcisiteData'));
	var items = pcidata[i];
	document.getElementById('pciseachText').value = items.cellName;
	localStorage.setItem('pcicellinfo', JSON.stringify(items)); // 保存选中的数据

}

// 点击pci 页面查询
function pciChacun() {
	if(null == document.getElementById("pciseachText").value || '' == document.getElementById("pciseachText").value) {
		return;
	}
	var pcicellinfo = JSON.parse(localStorage.getItem('pcicellinfo'));
	var locationData = JSON.parse(localStorage.getItem('projectData'));
	var logdata = JSON.parse(localStorage.getItem('LoginData'));
	var token = logdata.token;
	var inputTest = $("#pciseachText").val();
	var distance = $("#fanweiinput").val();
	var projectId = locationData.projectId;
	var ci = pcicellinfo.ci;
	//	"distance": "10000",
	//	"token": "BFFC458D3F3647F4AE1903168AE606B1",
	//	"ci": "34054021",
	//	"projectId": "1"

	$.ajax({
		type: "POST",
		url: url_prefix + "getSamePci.action?",
		dataType: 'JSON',
		data: {
			'request': "{\"distance\":\"" + distance + "\",\"token\":\"" + token + "\",\"ci\":\"" + ci + "\",\"projectId\":\"" + projectId + "\"}"

		},
		success: function(data) {
			var str1 = data["result"]; //解密

			if(str1.head.status == 1) {
				pcitongpindata(str1.data);
				//				localStorage.setItem('Details', JSON.stringify(str1.data.parts));
				console.log(str1);
				if(str1.data.length < 1) {
					mui.toast("当前范围无数据");
				}

			} else {
				alert(str1.head.info);
			}
		}
	});
}

// 搜索的同频数据处理
function pcitongpindata(data) {
	localStorage.setItem('tongpinData', JSON.stringify(data));
	var tongpindatadiv = document.getElementById('tongpindatadiv');
	if(data.length > 0) {
		tongpindatadiv.style.display = 'block';
	}

	if(data.length * 35 > 100) {
		tongpindatadiv.style = "overflow-x:hidden;overflow-y:auto;height: 100px;"
	}

	// 将第一行加上pci 和频点
	var selectcelldata = JSON.parse(localStorage.getItem('pcicellinfo'));
	var dic = {
		'ci': "pci:" + selectcelldata.pci,
		'cellName': "频点:" + selectcelldata.earfcn
	};
	data.unshift(dic);

	function setDiv(item, i) {
		var div = '<div id="' + (i - 1) + '" onclick="pcitongpinselect1(' + (i - 1) + ')" class="mohucx"><p>' +
			item.ci + "&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp" + item.cellName +
			'</p></div>'
		return div;
	}

	function getnoApplicationData() {
		var html = ''
		for(var i = 0; i < data.length; i++) {
			html += setDiv(data[i], i)
		}
		tongpindatadiv.innerHTML = html
	}
	window.onload = getnoApplicationData();
	pciselectMap(data);
}

// 搜索的出来的同频数据选择
function pcitongpinselect(i) {

	var tongpinData = JSON.parse(localStorage.getItem('tongpinData'));
	var itemtongpin = tongpinData[i];
	var item = new Array();
	item.push(itemtongpin);
	pciselectMap(item);
}

function pcitongpinselect1(i) {

	if(i != -1) {
		var tongpinData = JSON.parse(localStorage.getItem('tongpinData'));
		var itemtongpin = tongpinData[i];

		var item = new Array();
		item.push(itemtongpin);
		pciselectMap1(item);
	}

}